import { onMounted, ref } from 'vue';
import { getGood } from '@/api/good.service';
import { onLoad } from '@dcloudio/uni-app';

export function usePageList(promiseFun: Function, extraParams = {}) {
  const dataList = ref([]);
  const pagination = ref({});

  // 分页查询
  const queryListByPage = async (params = {}) => {
    const response = await promiseFun({
      page: 1,
      pageSize: 40,
      ...params,
      ...extraParams,
    });
    if (params.page && params.page > 1) {
      dataList.value = [...dataList.value, ...response.result];
      pagination.value = response.pagination;
    } else {
      dataList.value = response.result;
      pagination.value = response.pagination;
    }
  };

  const toLowerAction = () => {
    if (pagination.value?.page < pagination.value?.totalPage) {
      queryListByPage({
        page: pagination.value.page + 1,
      });
    }
  };
  const refreshAction = () => {
    queryListByPage();
  }

  
  onMounted(() => {
    queryListByPage();
  });
  return { dataList, toLowerAction, refreshAction };
}


export function useGood() {
  const good = ref({});
  onLoad(({ id }) => {
    getGood(Number(id)).then(({ result }) => {
      good.value = result;
    });
  });
  return {
    good
  };
}